package com.rs.controller;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.rs.model.CommonResult;
import com.rs.model.ZhOvertime;
import com.rs.model.ZhUser;
import com.rs.service.AuditInformationService;
import com.rs.util.DateChooserJButton;
import com.rs.util.DateFormatUtil;
import com.rs.util.SpringContextUtils;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class OvertimeInfo extends JFrame {

	private JPanel contentPane;
	private JTextField textField__username;
	private JTextField textField_timeLength;
	private JTextPane textPane_reason;
	private ZhUser user;
	private int id;
	
	private AuditInformationService audit = SpringContextUtils.getBean(AuditInformationService.class);
	private JTextField textField_ktime;
	private JTextField textField_jtime;
	
//	 DateChooserJButton dc= new DateChooserJButton();
//	 DateChooserJButton dc2= new DateChooserJButton();
	
	SelectOverTime ss;
	private JTextField textField_day;
	private Integer kk=null;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					OvertimeInfo frame = new OvertimeInfo();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public OvertimeInfo() {
		aa();
	}
	
	public OvertimeInfo(ZhUser user,int id,SelectOverTime ss) {
		this.ss=ss;
		this.user=user;
		this.id=id;
		aa();
	}
	
	void aa(){
	//	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 547, 482);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label_username = new JLabel("姓名：");
		label_username.setBounds(42, 108, 54, 15);
		contentPane.add(label_username);
		
		JLabel label_ktime = new JLabel("开始时间：");
		label_ktime.setBounds(42, 159, 76, 15);
		contentPane.add(label_ktime);
		
		JLabel label_jtime = new JLabel("结束时间：");
		label_jtime.setBounds(42, 208, 76, 15);
		contentPane.add(label_jtime);
		
		JLabel label_timeLength = new JLabel("加班时长：");
		label_timeLength.setBounds(42, 261, 76, 15);
		contentPane.add(label_timeLength);
		
		JLabel label_reason = new JLabel("加班原因：");
		label_reason.setBounds(253, 108, 76, 15);
		contentPane.add(label_reason);
		
		textField__username = new JTextField();
		textField__username.setBounds(107, 105, 104, 21);
		contentPane.add(textField__username);
		textField__username.setColumns(10);
		
		textField_timeLength = new JTextField();
		textField_timeLength.setColumns(10);
		textField_timeLength.setBounds(107, 258, 104, 21);
		contentPane.add(textField_timeLength);
		
		textPane_reason = new JTextPane();
		textPane_reason.setBounds(253, 144, 227, 145);
		contentPane.add(textPane_reason);
		
		JButton button_bh = new JButton("驳回");
		button_bh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				auditOvertime(2);
			}
		});
		button_bh.setBounds(251, 357, 76, 33);
		contentPane.add(button_bh);
		
		JButton button_tg = new JButton("通过");
		button_tg.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				auditOvertime(1);
			}
		});
		button_tg.setBounds(335, 357, 76, 33);
		contentPane.add(button_tg);
		
		JLabel label = new JLabel("加班审核");
		label.setFont(new Font("宋体", Font.PLAIN, 16));
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setBounds(204, 32, 90, 27);
		contentPane.add(label);
		
		textField_ktime = new JTextField();
		textField_ktime.setColumns(10);
		textField_ktime.setBounds(107, 156, 104, 21);
		contentPane.add(textField_ktime);
		
		textField_jtime = new JTextField();
		textField_jtime.setColumns(10);
		textField_jtime.setBounds(107, 205, 104, 21);
		contentPane.add(textField_jtime);
		
		textField_day = new JTextField();
		textField_day.setText("null");
		textField_day.setEditable(false);
		textField_day.setColumns(10);
		textField_day.setBounds(107, 306, 104, 21);
		contentPane.add(textField_day);
		
		JLabel label_day = new JLabel("加班日期：");
		label_day.setBounds(42, 309, 76, 15);
		contentPane.add(label_day);
		
		/*JPanel panel_ktime = new JPanel();
		panel_ktime.setBounds(107, 153, 104, 21);
		contentPane.add(panel_ktime);
		panel_ktime.add(dc);
		
		JPanel panel_jtime = new JPanel();
		panel_jtime.setBounds(107, 202, 104, 21);
		contentPane.add(panel_jtime);
		panel_jtime.add(dc2);		*/
		setTheValue( getOvertime());
		
	}
	
	//根据id得到用户加班的详细信息
	private ZhOvertime getOvertime(){
		return audit.getIdOvertime(id);
	}
	
	//渲染数据
	private  void setTheValue(ZhOvertime ov){
		textField__username.setText(ov.getOvAuname());
		textField_timeLength.setText(ov.getOvLength()+"");
		textPane_reason.setText(ov.getOvReason());
		textField_day.setText(DateFormatUtil.getDateStringw(ov.getOvKtime(), "yyyy-MM-dd"));
		textField_ktime.setText(DateFormatUtil.getDateStringw(ov.getOvKtime(), "HH:mm"));
		textField_jtime.setText(DateFormatUtil.getDateStringw(ov.getOvJtime(), "HH:mm"));
		
		textField__username.setEditable(false);
		textField_timeLength.setEditable(false);
		textPane_reason.setEditable(false);
		textField_ktime.setEditable(false);
		textField_jtime.setEditable(false);
		textField_day.setEditable(false);
		
	}
	
	//审核 通过或驳回
	private void auditOvertime(Integer ovaudit){
		ZhOvertime o= new ZhOvertime();
		o.setOvId(id);
		o.setOvAuid(user.getuId());
		o.setOvAudit(ovaudit);
		CommonResult<Integer> com  = audit.updateOvertimeAudit(o);
		if(com.getData()>0){
			alert("操作成功！");
			kk =ss.dynamicRefresh(id,kk);
		}
	}

	private void alert(String aa) {
		JOptionPane.showMessageDialog(this, aa);
	}
	
	
}
